<?php
/**
 * Template Name: Connects Page Template
 * Description: List links to all a persons friends
 *
 * @ Twyne
 */

get_header();?>
		
		<div id="primary">
		
			<div id="content" role="main">
				
				<?php  while ( have_posts() ) : 
						the_post();
						get_template_part( 'loop', 'front-page' ); //get the code from loop-front-page.php
					endwhile; // end of the loop.?>
				
					
					
					
<?php
/*
$post_titles = $wpdb->get_col("SELECT user_nicename FROM $wpdb->users");
foreach($post_titles as $posts){
echo $posts;
echo "<br />";
}
*/
?>

<img src="<?php bloginfo('stylesheet_directory'); ?>/connects-transparent.png" style="display: none;" /> 	
<?php
wp_get_current_user();
$userid = $current_user->ID;

$numfr = $wpdb->get_var("SELECT count(*)
						  FROM {$wpdb->prefix}bp_friends fr
						  WHERE fr.initiator_user_id = $userid");
$uid= $wpdb->get_col("SELECT Distinct um.user_id
						  FROM $wpdb->usermeta um, {$wpdb->prefix}bp_friends fr
						  WHERE fr.initiator_user_id = $userid AND um.user_id = fr.friend_user_id 
						  ORDER BY um.user_id");						  
$fname= $wpdb->get_col("SELECT meta_value
						  FROM $wpdb->usermeta um, {$wpdb->prefix}bp_friends fr
						  WHERE fr.initiator_user_id = $userid AND um.user_id = fr.friend_user_id AND 
						  um.meta_key = 'first_name'
						  ORDER BY um.user_id");
$lname= $wpdb->get_col("SELECT meta_value
						  FROM $wpdb->usermeta um, {$wpdb->prefix}bp_friends fr
						  WHERE fr.initiator_user_id = $userid AND um.user_id = fr.friend_user_id AND 
						  um.meta_key = 'last_name'
						  ORDER BY um.user_id");


if($numfr == 0){
	echo "You have not added anyone to your Connections <br/>";
	echo "To add someone to your Connects visit their profile <br/>";
	echo " and select 'Add to Connects'";
}else{

$full_name = array();
$connects = array();

for ($i=0; $i<$numfr; $i++){
	$full_name[$i] = $fname[$i] . " " . $lname[$i];
}

//stores names and ids and url
for ($i=0; $i<$numfr; $i++){
	$full_name[$i] = array ();
	$full_name[$i][0] = $uid[$i];
	$full_name[$i][1] = $fname[$i] . " " . $lname[$i];
}

 //sorts the first names
//prints user picture and link to profile
for ($i=0; $i<$numfr; $i++){
	$connects[$i] = $full_name[$i];
}

//sort by first names
    for ($i=0; $i<$numfr; $i++) {
        for ($j=0; $j<$numfr-1-$i; $j++) {
            if ($connects[$j+1][1] < $connects[$j][1]) {
                $tmp = $connects[$j+1][1];
				$tmp2 = $connects[$j+1][0];
				$connects[$j+1][1] = $connects[$j][1];
				$connects[$j+1][0] = $connects[$j][0];
				$connects[$j][1] = $tmp;
				$connects[$j][0] = $tmp2;
            }
        }
    }
//displays friends profile pics and names linked to their profiles
for ($i=0; $i<$numfr; $i++){
	$id	 = $connects[$i][0];
	$url = bp_core_get_user_domain($id);
	$url_name = $connects[$i][1];
	$avatar = bp_core_fetch_avatar ( array( 'item_id' => $connects[$i][0], 'type' => 'full' ) );
	
?>

<div id="profile_avatar">

<?php
	echo  "<a href='$url'>$url_name</a>" . "</br>";
	echo  "<a href='$url'>$avatar</a>" . "</br>";
?>
</div>
<?php	
	echo "<center><button type='button' onClick='remove($id,$userid);'>Remove Connect</button></center>";
	echo "</br>" . "</br>" ."</br>" ."</br>" ."</br>";
}
}
?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js "></script>
<script type="text/javascript">
	function remove(id,userid)
	{
		var fid = id;
		var uid	= userid;
		$.post(ajaxurl, {'action': 'removeConnect', 'fid': fid, 'uid' : uid});
		location.reload();
		
		
	}
</script>


				
			</div><!-- #content -->
			
		</div><!-- #primary -->

<?php 
	get_sidebar('empty');
	get_footer(); ?>

